package com.ks.phone.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.ks.phone.manager.CallPhoneManager;

public class PhoneStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取电话状态
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            // 电话空闲（挂断）
            // 可以在这里执行一些挂断电话时需要的操作
            Log.d("zhxk", "onCallStateChanged: 电话挂断");
            CallPhoneManager.getInstance().startTimer();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    CallPhoneManager.getInstance().requestHttpSaveCallInfo();
                }
            }, 5 * 1000);
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // 电话接通
            CallPhoneManager.getInstance().stopTimer();
            Log.d("zhxk", "onCallStateChanged: 电话接通");
        }
    }
}